package luccy.util;

import java.beans.Introspector;
import java.lang.reflect.Method;

public abstract class JavaBeansUtil {

	private JavaBeansUtil() {
	}

	public static String capitalize(String name) {
		if (StringUtil.isEmpty(name)) {
			return name;
		}
		char chars[] = name.toCharArray();
		chars[0] = Character.toUpperCase(chars[0]);
		return new String(chars);
	}

	public static String decapitalize(String name) {
		return Introspector.decapitalize(name);
	}

	public static String getPropertyName(String methodName) {
		if(methodName == null) {
			return null;
		}
		if(methodName.startsWith("get") || methodName.startsWith("set")) {
			String s = methodName.substring(3, methodName.length());
			return decapitalize(s);
		} else if(methodName.startsWith("is")) {
			String s = methodName.substring(2, methodName.length());
			return decapitalize(s);
		} else {
			return null;
		}
	}
	
	public static boolean isGetMethod(String methodName, Method m) {
		if (!methodName.startsWith("get")) {
			return false;
		}
		return (m.getParameterTypes().length == 0 && !methodName
				.equals("getClass"));
	}

	public static boolean isSetMethod(String methodName, Method m) {
		if (!methodName.startsWith("set")) {
			return false;
		}
		return (m.getParameterTypes().length == 1 && !methodName
				.equals("setClass"));
	}

	public static boolean isIsMethod(String methodName, Method m) {
		if (!methodName.startsWith("is")) {
			return false;
		}
		return (m.getParameterTypes().length == 0 && m.getReturnType().equals(
				Boolean.TYPE));
	}
}
